package com.yc.cloud.security.utils;

import com.yc.cloud.security.dto.AdminUserDetails;
import com.yc.cloud.entity.UmsRole;
import lombok.val;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;

/**
 * @title: AdminUtils
 * @Author JackGu
 * @Date: 2022/11/12 19:15
 * @Version 1.0
 */
public class AdminUtils {

    /** 超级管理员角色ID */
    public static final long SUPER_ADMIN_ROLE_ID = 1L;

    private static AdminUserDetails getUserDetail() {
        val authentication = SecurityContextHolder.getContext().getAuthentication();
        return (AdminUserDetails) authentication.getPrincipal();
    }

    public static String getCurrentUserName() {
        return getUserDetail().getUsername();
    }

    public static Long getCurrentUseId() {
        return getUserDetail().getUmsAdmin().getId();
    }

    /**
     * 判断是否包含超级管理员角色（根据角色ID列表）
     */
    public static boolean isSuperAdminByRoleIds(Collection<Long> roleIds) {
        if (roleIds == null) {
            return false;
        }
        for (Long roleId : roleIds) {
            if (roleId != null && roleId == SUPER_ADMIN_ROLE_ID) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否包含超级管理员角色（根据角色实体列表）
     */
    public static boolean isSuperAdminByRoles(Collection<UmsRole> roles) {
        if (roles == null) {
            return false;
        }
        for (UmsRole role : roles) {
            if (role != null && role.getId() != null && role.getId() == SUPER_ADMIN_ROLE_ID) {
                return true;
            }
        }
        return false;
    }
}
